package com.five.community.controller;

import com.five.community.bean.Housing;
import com.five.community.bean.Person;
import com.five.community.bean.Village;
import com.five.community.bean.vo.PersonVo;
import com.five.community.service.HousingService;
import com.five.community.service.PersonService;
import com.five.community.service.VillageService;
import com.five.community.util.FileUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/person")
public class PersonController {
    //显示人员页面
    @Autowired
    private PersonService personService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private HousingService housingService;
    @RequestMapping("/listUi")
    public String listUi(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         Model model,ModelMap map){
        try {

            List<PersonVo> pages= personService.selectAllPerson(pageNum,pageSize);
            List<PersonVo> persons = personService.selectAllPerson();
            PageInfo<PersonVo> pageInfo = new PageInfo<>(pages);
            model.addAttribute("pageInfo",pageInfo);
            map.addAttribute("persons",persons);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "person";
    }

    //添加画面跳转
    @RequestMapping("/addUi")
    public String addUi(ModelMap map1){
        try {
            List<Village> villages = villageService.selectAll();
            map1.addAttribute("villages",villages);
            List<Housing> housings = housingService.selectAll();
            map1.addAttribute("housings",housings);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "person_add";
    }

   //添加方法
    @RequestMapping("/add")
    public String add(PersonVo personVo, MultipartFile photos){
        long id1 = Long.parseLong(personVo.getVname());
        long id2 = Long.parseLong(personVo.getHname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        personVo.setPhoto(fileName);
        personVo.setCreateTime(new Date());
        try {
            Person person = new Person(id1,id2,personVo.getName(),fileName,
                    personVo.getIdentificationNum(),personVo.getTel(),
                    personVo.getJob(),personVo.getBirthday(),personVo.isGender(),personVo.getType(),
                    personVo.getRemark(),new Date());
            personService.insert(person);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/person/addUi";
    }
    //删除方法
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            personService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/person/listUi";
    }
    //更新页面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map1){
        try {
           Person person = personService.selectById(id);
            List<Village> villages = villageService.selectAll();
            List<Housing> housings = housingService.selectAll();
            map1.addAttribute("person",person);
            map1.addAttribute("villages",villages);
            map1.addAttribute("housings",housings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "/person_edit";
    }
    //更新方法
    @RequestMapping("/edit")
    public String edit(PersonVo personVo, MultipartFile photos){
        long id3 = Long.parseLong(personVo.getVname());
        long id4 = Long.parseLong(personVo.getHname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        personVo.setPhoto(fileName);
        personVo.setCreateTime(new Date());
        try {
            Person person = new Person(personVo.getId(), id3,id4,personVo.getName(),fileName,
                    personVo.getIdentificationNum(),personVo.getTel(),
                    personVo.getJob(),personVo.getBirthday(),personVo.isGender(),personVo.getType(),
                    personVo.getRemark(),new Date());
            personService.update(person);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "person_edit";
    }
    //搜索功能
    @RequestMapping("/select")
    public String select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         String name,ModelMap map,Model model) {
        if (name == "") {
            return "redirect:/person/listUi";
        } else {
            try {
                List<PersonVo> pages= personService.selectAllByName(pageNum,pageSize,name);
                List<PersonVo> persons = personService.selectByName(name);
                PageInfo<PersonVo> pageInfo = new PageInfo<>(pages);
                model.addAttribute("pageInfo",pageInfo);
                map.addAttribute("persons", persons);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "person";
        }
    }
    }

